﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace DAO
{
    public class StoreUtilities
    {
        /// <summary>
        /// Tao một command gọi store procedure
        /// </summary>
        /// <param name="cnn">connection</param>
        /// <param name="storeName">tên của store procedure</param>
        /// <returns></returns>
        public static SqlCommand CreateStoreCommand(SqlConnection cnn, String storeName)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.CommandText = storeName;
            cmd.Connection = cnn;
            return cmd;
        }

        /// <summary>
        /// Them một input parameter vào store command
        /// </summary>
        /// <param name="cmd"></param>
        /// <param name="paraName"></param>
        /// <param name="type"></param>
        /// <param name="size"></param>
        /// <param name="value"></param>
        public static void AddInputPara(SqlCommand storeCmd, String paraName, SqlDbType type, int size, Object value)
        {
            SqlParameter para = new SqlParameter(paraName, type, size);
            para.Direction = ParameterDirection.Input;
            para.Value = value;
            storeCmd.Parameters.Add(para);
        }

        /// <summary>
        /// Thêm một output parameter vào store command.
        /// </summary>
        /// <param name="storeCmd"></param>
        /// <param name="paraName"></param>
        /// <param name="type"></param>
        /// <param name="size"></param>
        public static void AddOutputPara(SqlCommand storeCmd, String paraName, SqlDbType type, int size)
        {
            SqlParameter para = new SqlParameter(paraName, type, size);
            para.Direction = ParameterDirection.Output;
            storeCmd.Parameters.Add(para);
        }
    }
}
